<?php
/**
 * Created by PhpStorm.
 * User: luohaixiong
 * Date: 17/3/7
 * Time: 下午2:18
 */

$dir = "./test/test";
$filename = $dir . "/test.text";


if(!is_dir($dir)){
    mkdir($dir, 0777, true);
}
clearstatcache();

if(!file_exists($filename) && !is_dir($filename)) {
    $file = fopen($filename, "w");
    if ($file) {
        fwrite($file, "the first line" . PHP_EOL);
        fclose($file);
    } else {
        echo "创建文件失败";
    }
}else{
    $file = fopen($filename, "a+");
    $writable = is_writable($filename);
    if($writable){

        fseek($file, -1 , SEEK_END);
        $c = fgetc($file);
        echo "文件指针位置", ftell($file), PHP_EOL;
        $text = "the add text";
        if($c != PHP_EOL){
            $text .= PHP_EOL;
        }else{
            echo "文件末尾是换行符", PHP_EOL;
        }

        echo "写入文件" . PHP_EOL;
        $res = fwrite($file, $text);

        echo ftell($file);

        fclose($file);
    }
}
?>